package com.ajk.server.bkcontroller;

import java.security.Principal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ajk.server.controller.access.ArgIndexForPrintLog;
import com.ajk.server.model.TreeMenu;
import com.ajk.server.model.query.TreeMenuQuery;
import com.ajk.server.model.request.UserMenuRequest;
import com.ajk.server.model.response.ResultList;
import com.ajk.server.model.response.ResultObject;
import com.ajk.server.service.TreeMenuService;
import com.ajk.server.service.UsersService;
import com.ajk.server.utils.ServiceAssert;

@RestController
@RequestMapping("/admin/menu")
public class AdminTreeMenuController {

	@Autowired
	UsersService usersService;
	
	@Autowired
	TreeMenuService treeMenuService;
	
	/**
	 * 拉取菜单
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResultList<TreeMenu> listMenu(TreeMenuQuery query){
		
		ResultList<TreeMenu> list = new ResultList<>();
		
		List<TreeMenu> menuList = treeMenuService.findTreeMenuByQuery(query);
		
		Integer count = treeMenuService.countTreeMenuByQuery(query);
		
		query.setCount(count);
		
		list.setItemList(menuList);
		
		list.setExt(query);
		
		return list;
	}
	
	/**
	 * 添加菜单
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResultObject<Integer> addTreeMenu(@RequestBody TreeMenu menu, Principal principal) {
		
		Integer id = treeMenuService.insertTreeMenu(menu);
		
		return new ResultObject<>(id);
	}
	
	/**
	 * 修改菜单
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/modify", method = RequestMethod.PUT)
	@ResponseBody
	public ResultObject<Integer> modifyTreeMenu(@RequestBody TreeMenu menu, Principal principal) {
		
		ServiceAssert.throwException(menu.getMenuId()==null || menu.getMenuId()<=0, "menuId不能为空");
		
		Integer id = treeMenuService.updateTreeMenu(menu);
		
		return new ResultObject<>(id);
	}
	
	/**
	 * 删除菜单
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/delete/{menuId}", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultObject<Integer> deleteTreeMenu(@PathVariable("menuId") Integer menuId) {
		
		Integer count = treeMenuService.deleteTreeMenuById(menuId);
		
		return new ResultObject<>(count);
	}
	
	/**
	 * 分配菜单给用户
	 */

	/**
	 * 添加菜单
	 */
	@ArgIndexForPrintLog(indexes = { 0 })
	@RequestMapping(value = "/distribution", method = RequestMethod.POST)
	@ResponseBody
	public ResultObject<Integer> distributionTreeMenu(@RequestBody UserMenuRequest userMenuRequest, Principal principal) {
		
		Integer id = treeMenuService.distributionTreeMenu(userMenuRequest);
		
		return new ResultObject<>(id);
	}
	
	
	
	
	/**
	 * 拉取管理员用户
	 */
	
	
}
